package com.ebocy.hammer.world.resourceManage;

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

import com.ebocy.hammer.core.util.Config;
import com.ebocy.hammer.core.util.ServerAddress;

/**
 * @author Qingrong Hou 服务器连接数计数器 May 15, 2012
 */
public class ConnectionCount {
	private static Map<String, AtomicInteger> gameServerCount;
	private static Map<String, AtomicInteger> battleServerCount;

	static {
		gameServerCount = new ConcurrentHashMap<String, AtomicInteger>();
		for (String ip: Config.gameServerMap.keySet()) {
			gameServerCount.put(ip, new AtomicInteger(0));
		}

		battleServerCount = new ConcurrentHashMap<String, AtomicInteger>();
		for (String ip: Config.battleServerMap.keySet()) {
			battleServerCount.put(ip, new AtomicInteger(0));
		}
	}

	public static ServerAddress getGameServerAddress() {
		for (Entry<String, AtomicInteger> entry : gameServerCount.entrySet()) {
			if (entry.getValue().get() < Config.gameServerMap.get(entry.getKey()).getCapacity())
				return Config.gameServerMap.get(entry.getKey());
		}
		return null;
	}

	public static ServerAddress getBattleServerAddress() {
		for (Entry<String, AtomicInteger> entry : battleServerCount.entrySet()) {
			if (entry.getValue().get() < Config.battleServerMap.get(entry.getKey()).getCapacity())
				return Config.battleServerMap.get(entry.getKey());
		}
		return null;
	}

	/**
	 * 获取连接数最少的服务器
	 */
	@Deprecated
	public static String getLowLoadServerIp() {
		String lowestOnlinePlayerServerIp = getLowestOnlinePlayerServerIp();
		return lowestOnlinePlayerServerIp;
	}

	/**
	 * 逻辑服务器在线人数增加计数
	 */
	public void increaseOnlinePlayer4GameServer(String ip) {
		AtomicInteger onlinePlayerCount = gameServerCount.get(ip);
		onlinePlayerCount.incrementAndGet();
	}

	/**
	 * 逻辑服务器在线人数减少计数
	 */
	public void decreaseOnlinePlayer4GameServer(String ip) {
		AtomicInteger onlinePlayerCount = gameServerCount.get(ip);
		onlinePlayerCount.decrementAndGet();
	}

	/**
	 * 战斗服务器在线人数增加计数
	 */
	public void increaseOnlinePlayer4BattleServer(String ip) {
		AtomicInteger onlinePlayerCount = battleServerCount.get(ip);
		onlinePlayerCount.incrementAndGet();
	}

	/**
	 * 战斗服务器在线人数减少计数
	 */
	public void decreaseOnlinePlayer4BattleServer(String ip) {
		AtomicInteger onlinePlayerCount = battleServerCount.get(ip);
		onlinePlayerCount.decrementAndGet();
	}

	/**
	 * 获取连接数最少的服务器
	 */
	private static String getLowestOnlinePlayerServerIp() {
		List<Entry<String, AtomicInteger>> list = new LinkedList<Entry<String, AtomicInteger>>(gameServerCount.entrySet());
		Collections.sort(list, new Comparator() {
			public int compare(Object o1, Object o2) {
				return ((Comparable) ((Entry) o1).getValue()).compareTo(((Entry) o2).getValue());
			}
		});

		return list.get(0).getKey();
	}
}
